<template>
  <div>
    <el-form :inline="true" :model="searchModelGetters" style="display: flex">
      <div v-for="search in searchData" :key="search.model">
        <el-form-item :label="search.label" >
          <el-select v-if="search.type==='select'" v-model="searchModelGetters[search.model]" :placeholder="search.label" filterable clearable>
            <el-option
              v-for="select in search.select"
              :label="select.name"
              :value="select.name"
              :key="select.id">
            </el-option>
          </el-select>
          <el-date-picker
            v-else-if="search.type==='date'"
            v-model="searchModelGetters[search.model]"
            clearable
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd">
          </el-date-picker>
          <el-input v-else v-model="searchModelGetters[search.model]" :placeholder="search.placeholder" clearable></el-input>
        </el-form-item>
      </div>
      <el-form-item>
        <el-button type="primary" @click="submitSearchModel">查询</el-button>
        <el-button type="primary" @click="clearSearchModel">清空条件</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'

export default {
  name: 'cement-search',
  props: {
    searchData: {
      type: Array
    }
  },
  methods: {
    submitSearchModel () {
      this.$emit('submitSearchModel')
    },
    initSearchModel () {
      this.searchData.forEach(search => this.$set(this.searchModelGetters, search.model, ''))
    },
    clearSearchModel () {
      this.initSearchModel()
      this.submitSearchModel()
    }
  },
  mounted () {
    this.initSearchModel()
  },
  computed: {
    ...mapGetters([
      'searchModelGetters'
    ])
  }
}
</script>
